import copy
import numpy as np
from collision_detecting_tools import is_overlap, mark_map


class Object:
    def __init__(self, object_type, color):
        self.type = object_type
        if self.type == 'm':
            self.color = color
            self.square_bounder_points_list = np.array([[10, 40], [10, 10], [40, 10], [40, 40]])
        if self.type == 'f':
            self.color = color
            self.square_bounder_points_list = np.array([[60, 40], [60, 10], [90, 10], [90, 40]])
        self.last_points_list = [(0, 0) for _ in range(8)]

    def update_square_points_list(self, step_move):  # step_move is a list, len==3, [left_right, up_down, angle]
        for i in range(4):
            self.square_bounder_points_list[i, 0] += step_move[0]
            self.square_bounder_points_list[i, 1] -= step_move[1]  # up is '-', down is '+'

        # rotation: 1 is ->, -1 is <-
        if step_move[-1] == -1:
            temp = copy.deepcopy(self.square_bounder_points_list[0, :])
            self.square_bounder_points_list[0, :] = self.square_bounder_points_list[3, :]
            self.square_bounder_points_list[3, :] = self.square_bounder_points_list[2, :]
            self.square_bounder_points_list[2, :] = self.square_bounder_points_list[1, :]
            self.square_bounder_points_list[1, :] = temp
        if step_move[-1] == 1:
            temp = copy.deepcopy(self.square_bounder_points_list[0, :])
            self.square_bounder_points_list[0, :] = self.square_bounder_points_list[1, :]
            self.square_bounder_points_list[1, :] = self.square_bounder_points_list[2, :]
            self.square_bounder_points_list[2, :] = self.square_bounder_points_list[3, :]
            self.square_bounder_points_list[3, :] = temp

        return 0

    def calculate_points_list(self, step_move, map_m, map_f):
        old_square_points_list = copy.deepcopy(self.square_bounder_points_list)
        self.update_square_points_list(step_move)    # get new_bounder_points_list

        points_list = [[0, 0] for _ in range(8)]
        if self.type == 'f':
            points_list[0] = self.square_bounder_points_list[0]
            points_list[1] = self.square_bounder_points_list[1]
            points_list[6] = self.square_bounder_points_list[2]
            points_list[7] = self.square_bounder_points_list[3]
            points_list[2] = self.square_bounder_points_list[1] * 2/3 + self.square_bounder_points_list[2] * 1/3
            points_list[5] = self.square_bounder_points_list[1] * 1/3 + self.square_bounder_points_list[2] * 2/3
            points_list[3] = self.square_bounder_points_list[0] * 2/3 + self.square_bounder_points_list[2] * 1/3
            points_list[4] = self.square_bounder_points_list[1] * 1/3 + self.square_bounder_points_list[3] * 2/3
        if self.type == 'm':
            points_list[0] = self.square_bounder_points_list[0]
            points_list[7] = self.square_bounder_points_list[3]
            points_list[1] = self.square_bounder_points_list[0] * 2/3 + self.square_bounder_points_list[1] * 1/3
            points_list[2] = self.square_bounder_points_list[0] * 2/3 + self.square_bounder_points_list[2] * 1/3
            points_list[3] = self.square_bounder_points_list[1] * 2/3 + self.square_bounder_points_list[2] * 1/3
            points_list[4] = self.square_bounder_points_list[1] * 1/3 + self.square_bounder_points_list[2] * 2/3
            points_list[5] = self.square_bounder_points_list[1] * 1/3 + self.square_bounder_points_list[3] * 2/3
            points_list[6] = self.square_bounder_points_list[2] * 1/3 + self.square_bounder_points_list[3] * 2/3

        map_self = mark_map(np.array(points_list).astype(int), 599, 599)
        if self.type == 'f':
            overlap = is_overlap(map_self, map_m)
        else:
            overlap = is_overlap(map_self, map_f)

        if overlap:  # collision happened
            self.square_bounder_points_list = old_square_points_list
            print("collision")
            return self.last_points_list
        else:
            return points_list

    def update_position(self, pygame, window, action, map_m, map_f):
        points_list = self.calculate_points_list(action, map_m, map_f)  # use square_points_list get real points_list
        pygame.draw.polygon(window, (255, 255, 255), list(self.last_points_list), 0)
        pygame.draw.polygon(window, self.color, points_list, 0)
        pygame.display.update()
        self.last_points_list = copy.deepcopy(points_list)

        return points_list
